import {getLightData, getZeroVipStatusApi, lightData} from "@/api/leaseOrder";

function formatTime (time,type='second') {
    let unixtime = time
    let unixTimestamp=''
    // 时间戳是秒,需要转成毫秒
    if(type=='second'){
        unixTimestamp = new Date(unixtime * 1000)
    }
    // 时间戳是毫秒
    if(type=='millisecond'){
       unixTimestamp = new Date(unixtime)
    }
    let Y = unixTimestamp.getFullYear()
    let M = ((unixTimestamp.getMonth() + 1) >= 10 ? (unixTimestamp.getMonth() + 1) : '0' + (unixTimestamp.getMonth() + 1))
    let D = (unixTimestamp.getDate() >= 10 ? unixTimestamp.getDate() : '0' + unixTimestamp.getDate())
    let H = (unixTimestamp.getHours() >= 10 ? unixTimestamp.getHours() : '0' + unixTimestamp.getHours())
    let Min = (unixTimestamp.getMinutes()) >= 10 ? unixTimestamp.getMinutes() : '0' + unixTimestamp.getMinutes()
    let S = (unixTimestamp.getSeconds() >= 10 ? unixTimestamp.getSeconds() : '0' + unixTimestamp.getSeconds())
    let toDay = Y + '-' + M + '-' + D +' '+ H + ':' + Min + ':' + S
    return toDay
}
// 租期账单状态
// 0:待支付 1:已支付 2:已逾期 3:已完成
let InstallmentStatus = {
    0:'unpaid',//待支付
    1:'paid',//已支付
    2:'paid_overdue',//已逾期
    3:'complete' //已完成
}
// 租赁状态
let leaseStatus = {
    0:'no_verify',//待审核
    100:'no_zhima',//待免押
    101:'no_contract',//待签约
    102:'contract', //签约中
    201:'contract_user', //用户签约中
    202:'contract_zu_user', //用户签约中(租赁宝)
    103:'no_pay', //待付款
    2:'no_delivery', //未发货
    3:'money_back', //申请退款中
    4:'take_goods', //待收货
    5:'lease', //租用中
    6:'complete', //已完成
    11:'not_written', //未核销
    52:'not_receive', //待接收（租完归还）
    '-1':'delete' //已删除,取消
}
// 订单状态颜色
function leaseStatusColor(status){
    let success=['lease','complete']
    let waiting=['no_verify','no_zhima','no_contract','contract','contract_user','contract_zu_user','no_pay','no_delivery','money_back']
    if(success.indexOf(status)>=0){
        return 'color:#3bc881;'
    }
    if(waiting.indexOf(status)>=0){
        return 'color:#ff9900;'
    }
}

const lightEventCode=['709','710','711']
import store from '@/store'
// 灯火,落地页 (进入落地页页面就要触发一次)
function lightsData(options){
    // 1，渠道参数
    if(options?.params){
        options={...options,...(JSON.parse(options.params))}
    }
    if(!options?.cno) return;


    // 2，额外参数
    let extraParams={
        user_id: store.state.app.uid,
        device: '', //手机型号
        terminal: '', //终端：安卓，苹果
        platform: '' // 小程序平台：支付宝，微信
    }
    try {
        const systemInfo = uni.getSystemInfoSync()
        console.log('设备信息', systemInfo)
        extraParams.device = systemInfo.model
        extraParams.terminal = systemInfo.platform // 终端
        extraParams.platform = systemInfo.app //小程序平台
    } catch (err) {
        console.error('获取设备信息失败', err)
    }
    console.log('额外参数',extraParams)

    // 3，整合参数
    let params={
        cei: options?.cp,
        cid: options?.cid,
        // query: options?.query,
        request_id: options?.rid,
        principal_id: options?.ppid,
        plan_id: options?.pnid,
        group_id: options?.gid,
        ad_id: options?.aid,
        os: options?.os,
        ts:options?.ts,
        id: options?.id,
        pid: options?.pid,
        oid: '',
        cno:options?.cno,
        code: options?.code,// 709进件／710审核通过／ 711发货
        ostatus:'', // 709进件／710审核通过／ 711发货
        ...extraParams
    }
    console.log('灯火参数',params)

    // 渠道参数保存：
    // getApp().globalData.lightParams=params
    // 渠道编码保存：创建订单接口
    getApp().globalData.cno=params?.cno

    lightData(params).then(res=>{
        if(res.status===200){
            // 保存灯火biz_no
            getApp().globalData.biz_no=res.data?.biz_no
        }
    })
}

// 灯火：下单回传
function getLightsData(params){
    // 注意：仅租赁触发事件才能调用GET请求绑定关系
    getLightData(params).then(res=>{
        let lightParams=res.data
        // 回传参数，标记为进件
        if(lightParams.code==709){
            lightParams.ostatus='进件'
            lightData(lightParams)
        }
    })
}

// 0元租包，是否开启
function getZeroVipStatus(){
    getZeroVipStatusApi().then(res=>{
        if(res.status==200){
            getApp().globalData.zeroVipStatus = res.data.status==1?true:false
        }
    })
}


export {formatTime,InstallmentStatus,leaseStatus,leaseStatusColor,lightsData,getLightsData,getZeroVipStatus}
